package com.xyht.sca_s.student_manage_system.modules.visitor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.SmsVisitorNeedKnow;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.dto.SmsVisitorNeedKnowDTO;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.vo.SmsVisitorNeedKnowVO;
import com.xyht.sca_s.student_manage_system.modules.visitor.mapper.SmsVisitorNeedKnowMapper;
import com.xyht.sca_s.student_manage_system.modules.visitor.service.SmsVisitorNeedKnowService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-07
 */
@Service
public class SmsVisitorNeedKnowServiceImpl extends ServiceImpl<SmsVisitorNeedKnowMapper, SmsVisitorNeedKnow> implements SmsVisitorNeedKnowService {
    @Autowired
    SmsVisitorNeedKnowMapper smsVisitorNeedKnowMapper;
    @Override
    public ResponseResult smsVisitorNeedKnowAdd(SmsVisitorNeedKnowVO smsVisitorNeedKnowVO) {
        if(isNullOrEmpty(smsVisitorNeedKnowVO.getVisitorKnow())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsVisitorNeedKnow smsVisitorNeedKnow = new SmsVisitorNeedKnow();
        smsVisitorNeedKnow.setVisitorKnow(smsVisitorNeedKnowVO.getVisitorKnow());
        smsVisitorNeedKnowMapper.insert(smsVisitorNeedKnow);
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateSmsVisitorNeedKnow(SmsVisitorNeedKnowVO smsVisitorNeedKnowVO) {
        if(isNullOrEmpty(smsVisitorNeedKnowVO)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsVisitorNeedKnow query = smsVisitorNeedKnowMapper.selectById(smsVisitorNeedKnowVO);
        if(isNullOrEmpty(query)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsVisitorNeedKnow smsVisitorNeedKnow = new SmsVisitorNeedKnow();
        BeanUtils.copyProperties(smsVisitorNeedKnowVO,smsVisitorNeedKnow);
        smsVisitorNeedKnowMapper.updateById(smsVisitorNeedKnow);
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectSmsVisitorNeedKnow() {
        SmsVisitorNeedKnow one = this.getOne(new LambdaQueryWrapper<SmsVisitorNeedKnow>()
                .orderByDesc(SmsVisitorNeedKnow::getUpdateTime)
                .last("limit 1")
        );
        SmsVisitorNeedKnowVO smsVisitorNeedKnowVO = BeanUtil.copyProperties(one, SmsVisitorNeedKnowVO.class);
        return CommonResult.success(smsVisitorNeedKnowVO);
    }

    @Override
    public ResponseResult selectSmsVisitorNeedKnowById(String id) {
        if(isNullOrEmpty(id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsVisitorNeedKnow query = smsVisitorNeedKnowMapper.selectById(id);
        if(isNullOrEmpty(query)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsVisitorNeedKnow smsVisitorNeedKnow = new SmsVisitorNeedKnow();
        smsVisitorNeedKnow.setId(id);

        SmsVisitorNeedKnowDTO smsVisitorNeedKnowDTO = new SmsVisitorNeedKnowDTO();

        BeanUtils.copyProperties(query,smsVisitorNeedKnowDTO);
        return CommonResult.success(smsVisitorNeedKnowDTO);
    }
}
